<template>
  <page-header-wrapper>
    <template v-slot:content>
      <div class="page-header-content">
        <div class="avatar">
          <a-avatar size="large" :src="currentUser.avatar" />
        </div>
        <div class="content">
          <div class="content-title">
            {{ timeFix }}，{{ user.name }}<span class="welcome-text"></span>
          </div>
          <div>
            上次登录地址：{{ user.lastLoginIp }} <br />
            上次登录时间：{{ user.lastLoginTime }}
          </div>
        </div>
      </div>
    </template>
    <template v-slot:extraContent>
      <div class="extra-content">
        <div class="stat-item">
          <a-statistic title="设备数" :value="deviceSum" />
        </div>
        <div class="stat-item">
          <a-statistic title="当前在线设备" :value="deviceOnline" />
        </div>
        <!-- <div class="stat-item">
          <a-statistic title="今日消息条数" :value="8" />
        </div> -->
        <!-- <div class="stat-item">
          <a-statistic title="项目访问" :value="2223" />
        </div> -->
      </div>
    </template>

    <div>
      <a-row :gutter="24">
        <a-col :xl="24" :lg="24" :md="24" :sm="24" :xs="24">
          <a-card
            class="project-list"
            :loading="loading"
            style="margin-bottom: 24px"
            :bordered="false"
            title="我的设备"
            :body-style="{ padding: 0 }"
          >
            <!-- <a slot="extra">全部设备</a> -->
            <div>
              <div v-if="devices.length == 0">
                <a-button @click="toAdd"  type="primary" block> 创建设备 </a-button>
              </div>
              <a-card-grid
                class="project-card-grid"
                :key="i"
                v-for="(item, i) in devices"
              >
                <a-card :bordered="false" @click="toDeviceDetail(item)" :body-style="{ padding: 0 }">
                  <a-card-meta>
                    <div slot="title" class="card-title">
                      <a-avatar size="small" :src="deviceAvatar" />
                      <a>{{ item.Name }}</a>
                    </div>
                    <div slot="description" class="card-description">
                      {{ item.Describe }}
                    </div>
                  </a-card-meta>
                  <div class="project-item" style="margin-top: 30px">
                    <a href="/#/">{{item.DeviceType.Name}}</a>
                    <span class="datetime">{{item.CreatedAt.split("T")[0]}}</span>
                  </div>
                </a-card>
              </a-card-grid>
            </div>
          </a-card>
        </a-col>
      </a-row>
    </div>
  </page-header-wrapper>
</template>

<script>
import { timeFix } from "@/utils/util";
import { mapState } from "vuex";
import { PageHeaderWrapper } from "@ant-design-vue/pro-layout";
import { Radar } from "@/components";

import { getAllDevices } from "@/api/device";

import deviceAvatar from '@/assets/switch.svg'

import { createConnection} from '@/utils/mqtt'

const DataSet = require("@antv/data-set");

export default {
  name: "Workplace",
  components: {
    PageHeaderWrapper,
    Radar,
  },
  data() {
    return {
      timeFix: timeFix(),
      avatar: "",
      deviceAvatar:deviceAvatar,
      user: {},
      deviceSum:0,
      deviceOnline:0,
      loading: true,
      radarLoading: true,

      devices: [],
    };
  },
  computed: {
    ...mapState({
      nickname: (state) => state.user.nickname,
      welcome: (state) => state.user.welcome,
    }),
    currentUser() {
      return {
        name: this.userInfo.name,
        avatar:
          "https://oss.aliyuncs.com/aliyun_id_photo_bucket/default_handsome.jpg",
      };
    },
    userInfo() {
      return this.$store.getters.userInfo;
    },
  },
  created() {
    this.user = this.userInfo;
    this.avatar = this.userInfo.avatar;
    console.log("发起连接")
    createConnection()
   
  },
  mounted() {
    this.getActivity();

    this.getDevices();
  },
  methods: {
    toDeviceDetail(device){
      this.$router.push({name:"deviceDetail",query: device})
    },
    toAdd(){
      this.$router.push({name:"AddDevice"})
    },
    getDevices() {
      getAllDevices().then((res) => {
        console.log(res);
        this.deviceSum = res.data.length
        this.devices = res.data
        this.deviceOnline = 0;
        this.devices.forEach(element => {
          if (element.Online){
            this.deviceOnline++;
          }
        });
        this.loading = false;
      });
    },
    getProjects() {
      this.$http.get("/list/search/projects").then((res) => {
        this.projects = res.result && res.result.data;
        this.loading = false;
      });
    },
    getActivity() {
      this.$http.get("/workplace/activity").then((res) => {
        this.activities = res.result;
      });
    },
    getTeams() {
      this.$http.get("/workplace/teams").then((res) => {
        this.teams = res.result;
      });
    },
  },
};
</script>

<style lang="less" scoped>
@import "./Workplace.less";

.project-list {
  .card-title {
    font-size: 0;

    a {
      color: rgba(0, 0, 0, 0.85);
      margin-left: 12px;
      line-height: 24px;
      height: 24px;
      display: inline-block;
      vertical-align: top;
      font-size: 14px;

      &:hover {
        color: #1890ff;
      }
    }
  }

  .card-description {
    color: rgba(0, 0, 0, 0.45);
    height: 44px;
    line-height: 22px;
    overflow: hidden;
  }

  .project-item {
    display: flex;
    margin-top: 8px;
    overflow: hidden;
    font-size: 12px;
    height: 20px;
    line-height: 20px;

    a {
      color: rgba(0, 0, 0, 0.45);
      display: inline-block;
      flex: 1 1 0;

      &:hover {
        color: #1890ff;
      }
    }

    .datetime {
      color: rgba(0, 0, 0, 0.25);
      flex: 0 0 auto;
      float: right;
    }
  }

  .ant-card-meta-description {
    color: rgba(0, 0, 0, 0.45);
    height: 44px;
    line-height: 22px;
    overflow: hidden;
  }
}

.item-group {
  padding: 20px 0 8px 24px;
  font-size: 0;

  a {
    color: rgba(0, 0, 0, 0.65);
    display: inline-block;
    font-size: 14px;
    margin-bottom: 13px;
    width: 25%;
  }
}

.members {
  a {
    display: block;
    margin: 12px 0;
    line-height: 24px;
    height: 24px;

    .member {
      font-size: 14px;
      color: rgba(0, 0, 0, 0.65);
      line-height: 24px;
      max-width: 100px;
      vertical-align: top;
      margin-left: 12px;
      transition: all 0.3s;
      display: inline-block;
    }

    &:hover {
      span {
        color: #1890ff;
      }
    }
  }
}

.mobile {
  .project-list {
    .project-card-grid {
      width: 100%;
    }
  }

  .more-info {
    border: 0;
    padding-top: 16px;
    margin: 16px 0 16px;
  }

  .headerContent .title .welcome-text {
    display: none;
  }
}
</style>
